home *** CD-ROM | disk | FTP | other *** search
- /*
- 1234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890
- | | | | | | | | | |
-
- */
-
- /*
- ** Oscilator.c
- **
- ** This file contains a simple oscilator for generating tones to pass off to the Sampled
- ** Sound synthisizer...
- **
- ** (NMD) 1/19/92 created
- **
- */
-
- #pragma load "MacHeaders"
-
-
-
- #ifndef __MAININCLUDES__
- #include "MainApp.h"
- #endif
-
- #include <SANE.h>
- #include <Math.h>
-
- #define kSampleRate 22000 // 22kHz Sample rate
-
- #define Long2X(x) Fix2X(Long2Fix(x))
- #define X2Long(x) Fix2Long(X2Fix(x))
-
- WaveCyclePtr NewWaveForm (unsigned char amplitude, unsigned short frequency)
- {
- WaveCyclePtr wave = nil; // return structure
- unsigned short cycleLength; // # of samples in cycle
- unsigned short sampleIterator; // iterate accross sample
- Ptr sampleArea = nil; // space to hold sample
-
- extended appxPI; // π approximation
- extended samplePt; // one sample point
- extended xAmp; // exteneded amplitude
- extended xOffset; // 0x80 in extended form
- extended inc; // radians/ iteration
-
- wave = (WaveCyclePtr) NewPtrClear (sizeof(WaveCycle));
-
- if (wave) {
- // First order of business: figure out how many samples there are in one cycle of a
- // wave at the given frequency at our current sample rate. the formula is as follows:
- //
- // (n Samples/Cycle) = (x Samples/Second) / (y Cycles/Second)
- cycleLength = kSampleRate / frequency;
-
- // Allocate space for new sample buffer
- sampleArea = NewPtrClear (cycleLength);
- Assert (sampleArea == nil, "\pCouldn't allocate space for the sample area");
- if (sampleArea != nil) {
-
- // Retrieve SANE's approximation of pi
- appxPI = pi();
-
- xOffset = (extended) 0x00000080;
-
- // Figure out how many radians apart each sample point is
- inc = (2*appxPI) / (extended)cycleLength;
-
- // convert the Amplitude to type extened so we can use it in the trig calculation
- xAmp = (extended)amplitude;
-
- for (sampleIterator = 0; sampleIterator < cycleLength; sampleIterator++) {
- samplePt = xAmp * sin (inc * (extended)sampleIterator) + xOffset;
- *(sampleArea + sampleIterator) = (long)samplePt;
- }
- }
- }
-
- wave->length = cycleLength;
- wave->wavePtr = sampleArea;
-
- return (wave);
- }
-